<?php

namespace Gateway;

class CommUtil
{
    /**
     * 获取随机字符串 (前面是时间戳)
     * @return string
     */
    public static function getTimeUUID() {
        $timeStr = CommUtil::getCurrentTimeStr();
        $randStr = CommUtil::GetRandStr(6);
        return $timeStr."_".$randStr;
    }

    /**
     * 获取当前时间戳， yyyyMMddHHmmssSSS 格式
     * @return string
     */
    public static function getCurrentTimeStr() {
        $timeStr = date('YmdHis', time()*1000);
        return $timeStr;
    }

    /**
     * 生成随机字符串
     * @param $length 字符串长度
     * @return string
     */
    public static function GetRandStr ($length) {
        //字符组合
        $str = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
        $len = strlen($str)-1;
        $randstr = '';
        for ($i=0;$i<$length;$i++) {
            $num=mt_rand(0,$len);
            $randstr .= $str[$num];
        }

        return $randstr;
    }
}
